﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;


namespace Oplaco.Core.Configuration
{
    /// <summary>
    /// Intializes system: loads configuration from config files and processes 
    /// </summary>
    public class SystemInitialization
    {
        #region members
        private static bool s_InitializedAlready = false;
        private static Object s_lock = new Object();
        private static object _settingsLock = new object();
        private static object _notificationLock = new object();

        private static SiteSettings _settings;
        private static DateTime _lastNotificationTime;
        private static string _siteUrl;
        #endregion

        #region Property
        
        #endregion

        #region Public
        public static SiteSettings ApplicationSettings
        {
            get
            {
                if (_settings == null) ApplicationSettings = SiteSettings.LoadFromConfiguration();
                return _settings;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("ApplicationSettings cannot be set to null");
                lock (_settingsLock)
                {
                    _settings = value;
                }
            }
        }

        public static DateTime LastNotificationSent
        {
            get
            {
                return _lastNotificationTime;
            }
            set
            {
                lock (_notificationLock)
                {
                    _lastNotificationTime = value;
                }
            }
        }

        public static string SiteUrl
        {
            get
            {
                return _siteUrl;
            }
        }
        
        public static void Initialize(HttpContext context)
        {
            if (s_InitializedAlready)
            {
                return;
            }
            lock (s_lock)
            {
                if (s_InitializedAlready)
                {
                    return;
                }
                // Perform first-request initialization here ...
                _settings = SiteSettings.LoadFromConfiguration();

                string baseUrl = null;                
                if (context != null)
                {
                    string port = context.Request.ServerVariables["SERVER_PORT"];
                    if (port == null || port.Equals("80") || port.Equals("443"))
                        port = String.Empty;
                    else
                        port = ":" + port;

                    string protocol = context.Request.ServerVariables["SERVER_PORT_SECURE"];

                    if (protocol == null || protocol.Equals("0"))
                        protocol = "http://";
                    else
                        protocol = "https://";

                    baseUrl = protocol + context.Request.ServerVariables["SERVER_NAME"] + port + context.Request.ApplicationPath;
                }
                _siteUrl = baseUrl;
                s_InitializedAlready = true;
            }
        }

        #endregion
        
       
    }
}
